package com.acce.gxsc.common;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.model.netBean.NetFeeInfo;
import com.acce.gxsc.model.netBean.NetOutCarBatchDetailBean;
import com.acce.sdk.BaseApp;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.widget.AcceBar;
import com.acce.sdk.widget.adapterrv.CommonAdapter;

import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.acce.gxsc.common.App.dutyId;
import static com.acce.gxsc.common.App.gateName;

/**
 * Created by huxley on 2017/6/7.
 */

public class Tools {

    public static final String EMPTY_STRING = "--";

    public static List<String> getStringList(int stringRes) {
        String[] stringArray = BaseApp.context.getResources().getStringArray(stringRes);
        return Arrays.asList(stringArray);
    }

    public static void setText(TextView view, String text) {
        if (StringUtils.isEmpty(text)) {
            view.setText("");
        } else {
            view.setText(text);
        }
    }

    public static void setText(TextView view, String text, String content) {
        if (StringUtils.isEmpty(text)) {
            view.setText(content);
        } else {
            view.setText(text);
        }
    }
    public static String getTimeStamp(String dateTime){
        return dateTime.substring(0,4)+dateTime.substring(5,7)+dateTime.substring(8,10)+
                dateTime.substring(11,13)+dateTime.substring(14,16)+dateTime.substring(17,19);
    }
    public static Bitmap convertViewToBitmap(View view, float scale) {
        //生成bitmap
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
        //利用bitmap生成画布
        Canvas canvas = new Canvas(bitmap);
        //把view中的内容绘制在画布上
        view.draw(canvas);
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    public static int getColor(int colorId) {
        return ContextCompat.getColor(App.context, colorId);
    }

    public static String getString1(String s1, String s2) {
        if (StringUtils.isEmpty(s1) && StringUtils.isEmpty(s2)) {
            return EMPTY_STRING + "/" + EMPTY_STRING;
        }
        s1 = StringUtils.isEmpty(s1) ? EMPTY_STRING : s1;
        s2 = StringUtils.isEmpty(s2) ? EMPTY_STRING : s2;
        return MessageFormat.format("{0}/{1}", s1, s2);
    }
    public static String getString1(String s1, String s2, String s3) {
        s1 = StringUtils.isEmpty(s1) ? EMPTY_STRING : s1;
        s2 = StringUtils.isEmpty(s2) ? EMPTY_STRING : s2;
        s3 = StringUtils.isEmpty(s3) ? EMPTY_STRING : s3;
        return MessageFormat.format("{0}/{1}/{2}", s1, s2, s3);
    }

    public static String getString4(String s1, String s2) {
        if (StringUtils.isEmpty(s1) && StringUtils.isEmpty(s2)) {
            return EMPTY_STRING + "_" + EMPTY_STRING;
        }
        s1 = StringUtils.isEmpty(s1) ? EMPTY_STRING : s1;
        s2 = StringUtils.isEmpty(s2) ? EMPTY_STRING : s2;
        return MessageFormat.format("{0}_{1}", s1, s2);
    }

    public static String getString2(String s1, String s2) {
        if (StringUtils.isEmpty(s1) && StringUtils.isEmpty(s2)) {
            return "";
        }
        s1 = StringUtils.isEmpty(s1) ? EMPTY_STRING : s1;
        s2 = StringUtils.isEmpty(s2) ? EMPTY_STRING : s2;
        return MessageFormat.format("{0}（{1}）", s1, s2);
    }


    private static int lastVisibleItem;

    public static RecyclerView.OnScrollListener getLoadMoreListener(final LinearLayoutManager layoutManager,
                                                                    final SwipeRefreshLayout swipeRefreshLayout,
                                                                    final CommonAdapter adapter,
                                                                    final OnLoadMoreListener loadMoreListener) {
        lastVisibleItem = 0;
        return new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (!swipeRefreshLayout.isRefreshing() && newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
                    loadMoreListener.loadMore();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        };
    }

    public static String getStringNonNull(String content) {
        if (StringUtils.isEmpty(content) || "null".equals(content)) {
            return "";
        }
        return content;
    }

    public static String getStringNonNull(String s1, String s2) {
        if (StringUtils.isEmpty(s1) && StringUtils.isEmpty(s2)) {
            return "";
        }
        s1 = StringUtils.isEmpty(s1) ? "" : s1;
        s2 = StringUtils.isEmpty(s2) ? "" : s2;
        return MessageFormat.format("{0}{1}", s1, s2);
    }

    public static void setAcceBar(AcceBar accebar) {
        accebar.setPageCode(dutyId);
        accebar.setCustomizeText(gateName);
        accebar.setCustomizeText(gateName);
    }

    public static void setStringWithTitle(TextView tv, String... content) {
        if (content.length == 1) {
            tv.setText("");
        } else if (content.length == 2) {
            if (StringUtils.isEmpty(content[1])) {
                tv.setText("");
            } else {
                tv.setText(MessageFormat.format("{0}", content[1]));
            }
        }
    }

    public static String getStayTime(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        try {
            long l = System.currentTimeMillis() - format.parse(time).getTime();
            return new DecimalFormat("#.#").format(l / 3600000.0) + "小时";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "0";
    }
    public static String getStayTime(String... time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        try {
            long l;
            if (time.length == 1) {
                l = System.currentTimeMillis() - format.parse(time[0]).getTime();
            } else {
                l = format.parse(time[1]).getTime() - format.parse(time[0]).getTime();
            }
            return "停留：" + new DecimalFormat("#.#").format(l / 3600000.0) + "h";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "0";
    }

    public static String getStayTime1(String... time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        try {
            long l;
            if (time.length == 1) {
                l = System.currentTimeMillis() - format.parse(time[0]).getTime();
            } else {
                l = format.parse(time[1]).getTime() - format.parse(time[0]).getTime();
            }
            return new DecimalFormat("#.#").format(l / 3600000.0);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "0";
    }

    public static String getString3(String containNo, String hangNum) {
        if (StringUtils.isEmpty(containNo) && StringUtils.isEmpty(hangNum)) {
            return "";
        } else if (StringUtils.isEmpty(containNo)) {
            return hangNum;
        } else if (StringUtils.isEmpty(hangNum)) {
            return containNo;
        } else {
            return MessageFormat.format("{0},{1}", containNo, hangNum);
        }
    }

    public static String getString4(String carNo, String boxNo, String hangNo, String remark) {
        if (StringUtils.isEmpty(carNo)) {
            carNo = EMPTY_STRING;
        }
        if (StringUtils.isEmpty(boxNo)) {
            boxNo = EMPTY_STRING;
        }
        if (StringUtils.isEmpty(hangNo)) {
            hangNo = EMPTY_STRING;
        }
        if (StringUtils.isEmpty(remark)) {
            remark = EMPTY_STRING;
        }
        return MessageFormat.format("{0}/{1}/{2}/{3}", carNo, boxNo, hangNo, remark);
    }

    public static String getString3(String carNo, String boxNo, String hangNo) {
        if (StringUtils.isEmpty(carNo)) {
            carNo = EMPTY_STRING;
        }
        if (StringUtils.isEmpty(boxNo)) {
            boxNo = EMPTY_STRING;
        }
        if (StringUtils.isEmpty(hangNo)) {
            hangNo = EMPTY_STRING;
        }
        return MessageFormat.format("{0}_{1}_{2}", carNo, boxNo, hangNo);
    }

    public static String getString4(String carNo, String boxNo, String hangNo, String area, String goods, String remark) {
        if (StringUtils.isEmpty(carNo)) {
            carNo = EMPTY_STRING;
        }
        if (StringUtils.isEmpty(boxNo)) {
            boxNo = EMPTY_STRING;
        }
        if (StringUtils.isEmpty(hangNo)) {
            hangNo = EMPTY_STRING;
        }
        if (StringUtils.isEmpty(area)) {
            area = EMPTY_STRING;
        }
        if (StringUtils.isEmpty(goods)) {
            goods = EMPTY_STRING;
        }
        if (StringUtils.isEmpty(remark)) {
            remark = EMPTY_STRING;
        }
        return MessageFormat.format("{0}/{1}/{2}/{3}/{4}/{5}", carNo, boxNo, hangNo, area, goods, remark);
    }

    public static boolean verificationId(String id) {
        return true;
//        if (id.equals(App.gateNo)) {
//            return true;
//        } else {
//            switch (id) {
//                case SUPERVISION_GATE_NO:
//                    ToastUtils.show("只有监察员才能进行该操作！");
//                    break;
//                case OUT_CAR_GATE_NO:
//                    ToastUtils.show("只有离场员才能进行该操作！");
//                    break;
//            }
//            return false;
//        }
    }

    public static String getOutCarState(String text) {
        if ("1".equals(text)) {
            return "已离场";
        } else {
            return "未离场";
        }
    }

    public static String getSupervisionState(String text) {
        if ("1".equals(text)) {
            return "已监察审核";
        } else {
            return "未监察审核";
        }
    }

    public static String getString(String text) {
        if (StringUtils.isEmpty(text)||text==null) {
            return EMPTY_STRING;
        } else {
            return text;
        }
    }

    public static String getChargeTotal(NetOutCarBatchDetailBean detailBean) {
        if (detailBean == null || detailBean.feeInfo == null || detailBean.feeInfo.isEmpty()) {
            return EMPTY_STRING;
        }
        double total = 0;
        for (NetFeeInfo fee : detailBean.feeInfo) {
            if (fee != null && !StringUtils.isEmpty(fee.feeSum)) {
                total += Double.valueOf(fee.feeSum);
            }
        }
//        if (detailBean == null || detailBean.inBatch == null || detailBean.inBatch.isEmpty()) {
//            return EMPTY_STRING;
//        }
//        double total = 0;
//        for (BatchBean batch : detailBean.inBatch) {
//            ArrayList<InCarGoodsBean> inGoods = batch.inGoods;
//            if (inGoods != null && !inGoods.isEmpty()) {
//                for (InCarGoodsBean goods : inGoods) {
//                    if (goods != null && !StringUtils.isEmpty(goods.chargeTotal) && !"-".equals(goods.chargeTotal)) {
//                        total += Double.valueOf(goods.chargeTotal);
//                    }
//                }
//            }
//        }
        return new DecimalFormat("#.##").format(total);
    }

    public static String getChargeTotal1(NetOutCarBatchDetailBean detailBean, String batchId) {
        if (detailBean == null || detailBean.feeInfo == null || detailBean.feeInfo.isEmpty()) {
            return EMPTY_STRING;
        }
        double total = 0;
        for (NetFeeInfo fee : detailBean.feeInfo) {
            if (fee != null && !StringUtils.isEmpty(fee.feeSum)) {
                if (batchId.equals(fee.inBatchId)) {
                    total += Double.valueOf(fee.feeSum);
                }
            }
        }
        return new DecimalFormat("#.##").format(total);
    }

    public static boolean getEnable(String fee2Id, String fee3Id) {
        return (!StringUtils.isEmpty(fee2Id) && ("1".equals(fee2Id) || fee2Id.startsWith("33"))) ||
                (!StringUtils.isEmpty(fee3Id) && ("1".equals(fee3Id) || fee3Id.startsWith("33")));
    }

    public static int getStateColor(String fee2Id, String fee3Id) {
        return getEnable(fee2Id, fee3Id) ? getColor(R.color.color_ccca43) : Color.WHITE;
    }

    public static String getOutCarStatus(String text) {
        if ("1".equals(text)) {
            return "已离场";
        } else {
            return EMPTY_STRING;
        }
    }

    public static String getSuperviseOthers(String text) {
        if ("1".equals(text)) {
            return "已监察审核";
        } else {
            return EMPTY_STRING;
        }
    }

    public static String getFee2Id(String feeId) {
        if (StringUtils.isEmpty(feeId)) {
            return "1#门单据";
        } else if ("0".equals(feeId)) {
            return "免门票";
        } else if ("1".equals(feeId)) {
            return "未收门票";
        } else if (feeId.startsWith("33")) {
            return "未收门票";
        } else if ("55".equals(feeId)) {
            return "已收门票";
        } else {
            return "1#门单据";
        }
    }

    public static String getFee3Id(String feeId) {
        if (StringUtils.isEmpty(feeId)) {
            return "1#门单据";
        } else if ("0".equals(feeId)) {
            return "免进场费";
        } else if ("1".equals(feeId)) {
            return "未收进场费";
        } else if (feeId.startsWith("33")) {
            return "未收进场费";
        } else if ("55".equals(feeId)) {
            return "已收进场费";
        } else {
            return "1#门单据";
        }
    }

    public static String getTime(String text) {
        if (StringUtils.isEmpty(text)) {
            return EMPTY_STRING;
        } else {
            return text.substring(5, 16);
        }
    }

    public static String getTime2(String text) {
        if (StringUtils.isEmpty(text)) {
            return EMPTY_STRING;
        } else {
            return text.substring(11, 16);
        }
    }

    public static String getTime1(String text) {
        if (StringUtils.isEmpty(text)) {
            return EMPTY_STRING;
        } else {
            return text.substring(0, 19);
        }
    }

    public static String getTime1(String text, String time) {
        if (StringUtils.isEmpty(text)) {
            return EMPTY_STRING;
        } else {
            return (text + " " + time).substring(0, 19);
        }
    }

    public static String getTime(String text, String time) {
        if (StringUtils.isEmpty(text)) {
            return EMPTY_STRING;
        } else {
            return (text + " " + time).substring(5, 16);
        }
    }

    public static String getPrice(String chargePrice, String chargeUnit) {
        if (StringUtils.isEmpty(chargePrice) || StringUtils.isEmpty(chargeUnit)) {
            return EMPTY_STRING;
        }
        return chargePrice + chargeUnit;
    }

    public static boolean equalsVersion(String oldVersion, String newVersion) {
        String[] oldSplit = oldVersion.split("\\.");
        String[] newSplit = newVersion.split("\\.");
        return (oldSplit.length < newSplit.length) ? equalsVersion(oldSplit, newSplit) : equalsVersion(newSplit, oldSplit);
    }

    private static boolean equalsVersion(String[] shortString, String[] longString) {
        int longInt, shortInt;
        for (int i = 0; i < shortString.length; i++) {
            longInt = StringUtils.isEmpty(longString[i]) ? 0 : Integer.parseInt(longString[i]);
            shortInt = StringUtils.isEmpty(shortString[i]) ? 0 : Integer.parseInt(shortString[i]);
            if (longInt != shortInt) {
                return false;
            }
        }
        return true;
    }

    private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    public static String getCurrTimeMillionsStr() {
        return sdf1.format(new Date());
    }

    public static String getBatchKey() {
        return getCurrTimeMillionsStr() + dutyId;
    }

    public static String getDutyString(String columnA, String columnB) {
        String bString = "";
        switch (columnB){
            case "1":
                bString = "早班";
                break;
            case "2":
                bString = "中班";
                break;
            case "3":
                bString = "晚班";
                break;
        }
        if (StringUtils.isEmpty(bString)) {
            bString =  EMPTY_STRING;
        }
        if (StringUtils.isEmpty(columnA)) {
            columnA =  EMPTY_STRING;
        }
        return MessageFormat.format("{0} {1}", columnA, bString);
    }
}
